//
//  HWWelfareCenterFuncView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWWelfareCenterFuncView: UIView {
    var itemViews:[HWWelfareFuncItemView] = []
    var lineView:UIView!

    
    
    var model:HWWelfareCenterModel! {
        didSet {
            for iv in itemViews {
                iv.removeFromSuperview()
            }
            itemViews.removeAll()
    
            var tempView:HWWelfareFuncItemView!
            for i in 0..<model.maindata.count {
                let itemView = HWWelfareFuncItemView()
                itemView.imageView.sd_setImage(with: URL(string: model.maindata[i].icon), completed: nil)
                itemView.titleLabel.text = model.maindata[i].name
                itemView.tag = i
                itemView.itemClick = {
                    self.itemClick?(self.model.maindata[itemView.tag])
                }
                addSubview(itemView)
                if tempView != nil {
                    if i % 4 == 0 {
                        itemView.snp.makeConstraints { make in
                            make.left.equalToSuperview()
                            make.top.equalTo(tempView.snp.bottom)
                            make.width.equalTo(screenWidth/4)
                            make.height.equalTo(92*widthScale)
                        }
                    }else {
                        itemView.snp.makeConstraints { make in
                            make.left.equalTo(tempView.snp.right)
                            make.top.equalTo(tempView)
                            make.width.equalTo(screenWidth/4)
                            make.height.equalTo(92*widthScale)
                        }
                    }
                }else {
                    itemView.snp.makeConstraints { make in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.equalTo(screenWidth/4)
                        make.height.equalTo(92*widthScale)
                    }
                }

                tempView = itemView
                itemViews.append(itemView)
            }
            
            if itemViews.count > 0 {
                itemViews.last?.snp.makeConstraints({ make in
                    make.bottom.equalToSuperview()
                })
                
            }
            
        }
    }
    
    
    
    var itemClick:((_:HWWelfareCenterDataModel)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension HWWelfareCenterFuncView {
    private func initUI() -> Void {
        backgroundColor = .white
    }
    
    private func initFrame() -> Void {
        
    }
}
